// 引入api文件  直接找里面的default
const api = require("../../utils/api.js").default;
// 引入封装好的请求方式
const myRequest = require("../../utils/request.js").default; 
// pages/shopcar/shopcar.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shopcargoods:[],
    totalMoney:0,
    checkAllState:0
  },

  //  点击 增加/减少 数量
  addReduceNum(e) {
    // 获取购物车数据
    var car_id = e.target.dataset.carmsg.id;
    var goods_id = e.target.dataset.carmsg.goods_id;
    var product_id = e.target.dataset.carmsg.product_id;
    var num = ++e.target.dataset.carmsg.number;
    
    var numState = e.target.dataset.numstate;
    var i = e.currentTarget.dataset.numindex;
    console.log(e.target.dataset.carmsg);
    //  增加
    if (numState === 'true' && this.data.shopcargoods[i].number >= 0){
      ++this.data.shopcargoods[i].number
      if (this.data.shopcargoods[i].checked === 0) {
        this.data.totalMoney = this.data.totalMoney + this.data.shopcargoods[i].market_price
        this.setData({
          totalMoney: this.data.totalMoney.toFixed(1)
        })
        console.log(this.data.totalMoney)
      }
      this.setData({
        shopcargoods: this.data.shopcargoods
      })
    }
    //  减少
    if (numState === 'false' && this.data.shopcargoods[i].number > 0) {
      --this.data.shopcargoods[i].number
      if (this.data.shopcargoods[i].checked === 0) {
        this.data.totalMoney = this.data.totalMoney - this.data.shopcargoods[i].market_price
        this.setData({
          totalMoney: this.data.totalMoney.toFixed(1)
        })
        console.log(this.data.totalMoney)
      }
      this.setData({
        shopcargoods: this.data.shopcargoods
      })
    }
    const token = wx.getStorageSync("token");
    wx.request({
        url: api.updatecar,
        method: "POST",
      header: {
        // 请求头
        "content-type": "application/json", //默认  
        "X-Nideshop-Token": token
      },
      data:{
        id: car_id,
        goodsId: goods_id,
        number: num,
        productId: product_id
      },
      success:function(res){
        console.log(res)
      }
    })
  },
  //  输入数量
  inputNun(e){
    // 获取购物车数据
    var car_id = e.target.dataset.carmsg.id;
    var goods_id = e.target.dataset.carmsg.goods_id;
    var product_id = e.target.dataset.carmsg.product_id;
    var num = e.detail.value;
    // 选中的下标
    var i = e.currentTarget.dataset.numindex;
    console.log(this.data.shopcargoods[i].number)
    // 如果输入不为数字，则变为0
    if (typeof e.detail.value == ""){
      e.detail.value = 0
    }
    this.data.shopcargoods[i].number = e.detail.value;
    console.log(this.data.shopcargoods[i].number)

    // 如果是选中状态,可以更改数值
    if (!this.data.shopcargoods[i].checked){
      this.data.totalMoney = 0
      for (var i = 0, len = this.data.shopcargoods.length; i < len; i++) {
        if (this.data.shopcargoods[i].checked === 0){
          this.data.totalMoney += this.data.shopcargoods[i].market_price * this.data.shopcargoods[i].number
        }
      }
      this.setData({
        totalMoney: this.data.totalMoney
      })
    }

    //  更新购物车
    var that = this;
    const token = wx.getStorageSync("token");
    wx.request({
      url: api.updatecar,
      method: "POST",
      header: {
        // 请求头
        "content-type": "application/json", //默认  
        "X-Nideshop-Token": token
      },
      data: {
        id: car_id,
        goodsId: goods_id,
        number: num,
        productId: product_id
      },
      success: function (res) {
        that.setData({
          shopcargoods: that.data.shopcargoods
        })
      }
    })
  },
  //  删除
  del(e){
    var i = e.currentTarget.dataset.delindex
    if (!this.data.shopcargoods[i].checked){
      this.data.totalMoney -= this.data.shopcargoods[i].market_price * this.data.shopcargoods[i].number
      this.setData({
        totalMoney:this.data.totalMoney
      })
    }
    
    var that=this;
    var delid = JSON.stringify(e.currentTarget.dataset.delid)
    const token = wx.getStorageSync("token");
    wx.request({
        url:api.del,
        method:"POST",
      header: {
        //  请求头
        "content-type": "application/json", //默认  
        "X-Nideshop-Token": token
      },
        data:{
          productIds: delid
        },
        success:function(res){
          console.log(res)
          that.setData({
            shopcargoods: res.data.data.cartList
          })       
        }
    })
  },
  //  选中
  cheItem(e){
    var i = e.target.dataset.numindex;
    //  更换选中状态
    if (this.data.shopcargoods[i].checked == 1 ) {
      this.data.shopcargoods[i].checked = 0
      this.setData({
        shopcargoods: this.data.shopcargoods
      })
    } else {
      this.data.shopcargoods[i].checked = 1
      this.setData({
        shopcargoods: this.data.shopcargoods,
        checkAllState:0
      })
    }
    //  选中与未选中的金额变动
    if(this.data.shopcargoods[i].checked === 0){
      this.data.totalMoney = this.data.totalMoney + this.data.shopcargoods[i].market_price * this.data.shopcargoods[i].number
      this.setData({
        totalMoney: this.data.totalMoney
      })
      console.log(this.data.totalMoney)
      //  判断是否全选
      var nowTotalMoney = 0
      for (var i = 0, len = this.data.shopcargoods.length; i < len; i++) {
        nowTotalMoney += this.data.shopcargoods[i].market_price * this.data.shopcargoods[i].number
      }
      if (this.data.totalMoney === nowTotalMoney) {
        this.setData({
          checkAllState: 1
        })
      }
    }else{
      this.data.totalMoney = this.data.totalMoney - this.data.shopcargoods[i].market_price * this.data.shopcargoods[i].number
      this.setData({
        totalMoney: this.data.totalMoney
      })
      console.log(this.data.totalMoney)
    }
    
  },
  // 全选
  cheItemAll(e){
    if (!this.data.checkAllState){
      for(var i=0,len=this.data.shopcargoods.length;i<len;i++){
        
        if (this.data.shopcargoods[i].checked){
          this.data.shopcargoods[i].checked = 0
          this.data.totalMoney += this.data.shopcargoods[i].market_price * this.data.shopcargoods[i].number  
          console.log(this.data.shopcargoods[i].checked)
        }
      }
      this.setData({
        shopcargoods: this.data.shopcargoods,
        totalMoney: this.data.totalMoney,
        checkAllState:1
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    self = this;
    const token = wx.getStorageSync("token");
    wx.request({
      url: api.getcar,
      header: {
        //  请求头
        "content-type": "application/json", //默认  
        "X-Nideshop-Token": token
      },
      success: function (res) {
        console.log(res)
        self.setData({
          shopcargoods: res.data.data.cartList
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
    for (var i = 0, len = this.data.shopcargoods.length;i<len;i++){
      this.data.shopcargoods[i].checked = 1  
    }
    this.setData({
      shopcargoods:this.data.shopcargoods,
      checkAllState: 0,
      totalMoney: 0
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})